//
//  NetWorkTools.swift
//  CLWeiBo
//
//  Created by 程雷 on 15/6/26.
//  Copyright © 2015年 程雷. All rights reserved.
//

import UIKit
import AFNetworking

class NetWorkTools: AFHTTPSessionManager {
    
    private static let instance:NetWorkTools = {
        
        // baseUrl 要以 '/'结尾
        let urlString = "https://api.weibo.com/"
        
        let baseUrl = NSURL(string: urlString)!
        let tools = NetWorkTools(baseURL:baseUrl)
         //设置相应的默认的反序列化格式
        tools.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as! Set<String>
        
        return tools
    }()
    
    // 全局访问方法
    class func sharedNetworkTools() ->NetWorkTools{
        
        return instance
    }
    
    // MARK :发布微博
    func postStatus(text:String,finished:(error:NSError?)->()){
        // 判断 token 是否有内容
        assert(sharedUserAccount?.access_token != nil,"必须传入 token")
        let urlString = "2/statuses/update.json"
        let params = ["access_token":sharedUserAccount!.access_token, "status": text]
        POST(urlString, parameters: params, success: { (_, JSON) -> Void in
            
            finished(error: nil)
            
            }) { (_, error) -> Void in
                print(error)
                finished(error: error)
        }
    }

}
